| Conditions | 1 |
| Paths | 1 |
| Total Lines | 129 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | describe('Model Generator', function () { |
||
| 2 | it('Person Model exist', function () { |
||
| 3 | expect(Test.model.Person).toBeDefined(); |
||
|
|
|||
| 4 | }); |
||
| 5 | it('Base Book Model to exist', function() { |
||
| 6 | expect(Test.model.BaseBook).toBeDefined(); |
||
| 7 | }); |
||
| 8 | it('Book Model to exist', function() { |
||
| 9 | expect(Test.model.Book).toBeDefined(); |
||
| 10 | }); |
||
| 11 | describe('Model Fields', function () { |
||
| 12 | var getField = (function () { |
||
| 13 | var fields = Test.model.Person.getFields(); |
||
| 14 | var list = {}; |
||
| 15 | for (var i = 0; i < fields.length; i++) { |
||
| 16 | list[fields[i].name] = fields[i]; |
||
| 17 | } |
||
| 18 | return function (name) { |
||
| 19 | return list[name]; |
||
| 20 | } |
||
| 21 | })(); |
||
| 22 | it('contain 9 fields', function () { |
||
| 23 | expect(Test.model.Person.getFields().length).toBe(9); |
||
| 24 | }); |
||
| 25 | it('age field', function () { |
||
| 26 | expect(getField('age').type).toBe(Ext.data.Types.INT); |
||
| 27 | }); |
||
| 28 | it('active field', function () { |
||
| 29 | expect(getField('active').type).toBe(Ext.data.Types.BOOLEAN); |
||
| 30 | }); |
||
| 31 | it('createdAt field', function () { |
||
| 32 | expect(getField('createdAt').type).toBe(Ext.data.Types.DATE); |
||
| 33 | }); |
||
| 34 | it('email field', function () { |
||
| 35 | expect(getField('email').type).toBe(Ext.data.Types.STRING); |
||
| 36 | }); |
||
| 37 | it('exclude dob field', function() { |
||
| 38 | expect(getField('dob')).toBeUndefined(); |
||
| 39 | }); |
||
| 40 | }); |
||
| 41 | describe('Model Validations', function() { |
||
| 42 | it('Presence Failed', function() { |
||
| 43 | var p = Ext.create('Test.model.Person'); |
||
| 44 | p.set("firstName", ""); |
||
| 45 | var errors = p.validate(); |
||
| 46 | expect(errors.getByField("firstName").length).toBe(1); |
||
| 47 | }); |
||
| 48 | it('Presence Success', function() { |
||
| 49 | var p = Ext.create('Test.model.Person'); |
||
| 50 | p.set("lastName", "test"); |
||
| 51 | var errors = p.validate(); |
||
| 52 | expect(errors.getByField("lastName").length).toBe(0); |
||
| 53 | }); |
||
| 54 | it('Length Failed', function() { |
||
| 55 | var p = Ext.create('Test.model.Person'); |
||
| 56 | p.set("email", "[email protected]"); |
||
| 57 | var errors = p.validate(); |
||
| 58 | expect(errors.getByField("email").length).toBe(1); |
||
| 59 | p.set("email", "[email protected]"); |
||
| 60 | errors = p.validate(); |
||
| 61 | expect(errors.getByField("email").length).toBe(1); |
||
| 62 | }); |
||
| 63 | it('Length Success', function() { |
||
| 64 | var p = Ext.create('Test.model.Person'); |
||
| 65 | p.set("email", "[email protected]"); |
||
| 66 | var errors = p.validate(); |
||
| 67 | expect(errors.getByField("email").length).toBe(0); |
||
| 68 | }); |
||
| 69 | it('Email Failed', function() { |
||
| 70 | var p = Ext.create('Test.model.Person'); |
||
| 71 | p.set("email", "as.ad.coma1.au"); |
||
| 72 | var errors = p.validate(); |
||
| 73 | expect(errors.getByField("email").length).toBe(1); |
||
| 74 | }); |
||
| 75 | it('Email Success', function() { |
||
| 76 | var p = Ext.create('Test.model.Person'); |
||
| 77 | p.set("email", "[email protected]"); |
||
| 78 | var errors = p.validate(); |
||
| 79 | expect(errors.getByField("email").length).toBe(0); |
||
| 80 | }); |
||
| 81 | it('Regex Success', function() { |
||
| 82 | var p = Ext.create('Test.model.Person'); |
||
| 83 | p.set("regex", "81 4"); |
||
| 84 | var errors = p.validate(); |
||
| 85 | expect(errors.getByField("regex").length).toBe(0); |
||
| 86 | }); |
||
| 87 | it('Regex Failed', function() { |
||
| 88 | var p = Ext.create('Test.model.Person'); |
||
| 89 | p.set("regex", "a1 4"); |
||
| 90 | var errors = p.validate(); |
||
| 91 | expect(errors.getByField("regex").length).toBe(1); |
||
| 92 | }); |
||
| 93 | it('Choice Success', function() { |
||
| 94 | var p = Ext.create('Test.model.Person'); |
||
| 95 | p.set("color", "red"); |
||
| 96 | var errors = p.validate(); |
||
| 97 | expect(errors.getByField("color").length).toBe(0); |
||
| 98 | }); |
||
| 99 | it('Choice Failed', function() { |
||
| 100 | var p = Ext.create('Test.model.Person'); |
||
| 101 | p.set("color", "green"); |
||
| 102 | var errors = p.validate(); |
||
| 103 | expect(errors.getByField("color").length).toBe(1); |
||
| 104 | }); |
||
| 105 | }); |
||
| 106 | describe('Model Associations', function() { |
||
| 107 | it('books define in person', function() { |
||
| 108 | var person = Ext.create('Test.model.Person'); |
||
| 109 | expect(person.books).toBeDefined(); |
||
| 110 | }); |
||
| 111 | it('person define in book', function() { |
||
| 112 | var book = Ext.create('Test.model.Book'); |
||
| 113 | expect(book.getPerson).toBeDefined(); |
||
| 114 | expect(book.setPerson).toBeDefined(); |
||
| 115 | }); |
||
| 116 | it('associate books to person', function() { |
||
| 117 | var book1 = Ext.create('Test.model.Book'); |
||
| 118 | book1.set('name', 'Book A'); |
||
| 119 | var book2 = Ext.create('Test.model.Book'); |
||
| 120 | book2.set('name', 'Book 2'); |
||
| 121 | var person = Ext.create('Test.model.Person', {id: 10}); |
||
| 122 | person.books().add(book1, book2); |
||
| 123 | expect(person.books().count()).toEqual(2); |
||
| 124 | person.books().remove(book2); |
||
| 125 | expect(person.books().count()).toEqual(1); |
||
| 126 | expect(book1.dirty).toBeTruthy(); |
||
| 127 | }) |
||
| 128 | }); |
||
| 129 | }); |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.